package com.zxw.limit;

import org.springframework.cloud.gateway.filter.ratelimit.AbstractRateLimiter;
import org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter;
import org.springframework.cloud.gateway.support.ConfigurationService;
import org.springframework.context.ApplicationEvent;
import org.springframework.validation.annotation.Validated;
import reactor.core.publisher.Mono;

import javax.validation.constraints.Min;

/**
 * @author zxw
 * @date 2021-01-11 14:29
 */
public class RedisRateLimit extends AbstractRateLimiter<RedisRateLimiter.Config> {


    protected RedisRateLimit(Class<RedisRateLimiter.Config> configClass, String configurationPropertyName, ConfigurationService configurationService) {
        super(configClass, configurationPropertyName, configurationService);
    }

    @Override
    public Mono<Response> isAllowed(String routeId, String id) {
        return null;
    }

    @Validated
    public static class Config {

        @Min(1)
        private int replenishRate;

        @Min(1)
        private int burstCapacity = 1;

        public int getReplenishRate() {
            return replenishRate;
        }

        public Config setReplenishRate(int replenishRate) {
            this.replenishRate = replenishRate;
            return this;
        }

        public int getBurstCapacity() {
            return burstCapacity;
        }

        public Config setBurstCapacity(int burstCapacity) {
            this.burstCapacity = burstCapacity;
            return this;
        }

        @Override
        public String toString() {
            return "Config{" + "replenishRate=" + replenishRate + ", burstCapacity="
                    + burstCapacity + '}';
        }

    }

}
